package com.masterclient.app.modules.alipush;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import com.eoner.baselib.utils.notify.RxBus;
import com.masterclient.app.MainActivity;
import com.masterclient.app.utils.CommonUtil;
import com.eoner.baselib.utils.activity.UtilActivity;
import com.google.gson.Gson;

import java.util.Map;

/**
 * @feature: 用于接收推送的通知和消息
 */
public class MyMessageReceiver extends MessageReceiver {

    // 消息接收部分的LOG_TAG
    public static final String REC_TAG = "===PUSH-receiver";
    private RxBus rxBus;

    @Override
    public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
        // TODO 处理推送通知
        if ( null != extraMap ) {
            for (Map.Entry<String, String> entry : extraMap.entrySet()) {
                Log.i(REC_TAG,"@Get diy param : Key=" + entry.getKey() + " , Value=" + entry.getValue());
            }
        } else {
            Log.i(REC_TAG,"@收到通知 && 自定义消息为空");
        }
        Log.i(REC_TAG,"收到一条推送通知 ： " + title + ", summary:" + summary);
    }


    @Override
    protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
        Log.i(REC_TAG,"onNotificationReceivedInApp ： " + " : " + title + " : " + summary + "  " + extraMap + " : " + openType + " : " + openActivity + " : " + openUrl);
        if (rxBus == null) {
            rxBus = RxBus.$();
        }
        if (UtilActivity.isCurActivity("MainActivity")) {
            CommonUtil.goAnyWhere(context, (extraMap!=null && extraMap.get("target") != null) ?extraMap.get("target"):"", CommonUtil.targetIdMultiFormat(extraMap), "", "", "");
        }else {
            Intent intent = new Intent(context, MainActivity.class);
            intent.putExtra("target", extraMap.get("target"));
            intent.putExtra("target_id", CommonUtil.targetIdMultiFormat(extraMap));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent, null);
        }
    }


    @Override
    public void onMessage(Context context, CPushMessage cPushMessage) {
        Log.i(REC_TAG,"收到一条推送消息 ： " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
    }


    @Override
    public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
        Log.i(REC_TAG,"onNotificationOpened ： " + " : " + title + " : " + summary + " : " + extraMap);

        Map<String, String> map = new Gson().fromJson(extraMap, Map.class);
        if (rxBus == null) {
            rxBus = RxBus.$();
        }
        CommonUtil.goAnyWhere(context,(map!=null && map.get("target") != null) ?map.get("target"):"", CommonUtil.targetIdMultiFormat(map), "", "", "");


    }

    @Override
    public void onNotificationRemoved(Context context, String messageId) {
        Log.i(REC_TAG, "onNotificatinRemoved ： " + messageId);
    }


    @Override
    protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
        Log.i(REC_TAG,"onNotificationClickedWithNoAction ： " + " : " + title + " : " + summary + " : " + extraMap);
    }
}